package dao.impl;

import dao.IBusinessManagementDao;
import pojo.Businesses;
import util.DBHelper;
import util.PageHelper;

import java.util.ArrayList;

//业务管理
public class BusinessManagementDao implements IBusinessManagementDao {
    private DBHelper db = new DBHelper();

    //业务查看(动态模糊查询,带分页)
    @Override
    public ArrayList<Businesses> selectBusinesses(Businesses businesses, PageHelper pageHelper) {
        StringBuilder sql = new StringBuilder("select * from businesses where 1=1");
        ArrayList list = new ArrayList();
        if (businesses.getbName() != null) {
            sql.append(" and bName like ?");
            list.add("%" + businesses.getbName() + "%");
        }
        sql.append(" LIMIT ?, ?");
        list.add(pageHelper.getStartIndex());
        list.add(pageHelper.getShowNumber());
        return db.dtQuery(sql.toString(), Businesses.class, list.toArray());
    }

    //业务查看
    @Override
    public ArrayList<Businesses> selectAllBusinesses() {
        StringBuilder sql = new StringBuilder("select * from businesses");
        return db.dtQuery(sql.toString(), Businesses.class);
    }

    @Override
    public ArrayList<Businesses> selectBusinessById(Integer bId) {
        String sql="select * from businesses where bId=?";
        return db.dtQuery(sql,Businesses.class,bId);
    }

    //无条件全数据查询
    @Override
    public int getTotalCount(Businesses businesses) {
        StringBuilder sql = new StringBuilder("select count(*) from businesses where 1=1");
        ArrayList list = new ArrayList();
        if (businesses.getbName() != null) {
            sql.append(" and bName like ?");
            list.add("%" + businesses.getbName() + "%");
        }
        try {
            Object result = db.executeScalar(sql.toString(), list.toArray());
            return result != null ? Integer.parseInt(result.toString()) : 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    //创建业务(参数为业务对象)
    @Override
    public int insertBusiness(Businesses businesses) {
        String sql = "insert into businesses(bName,status,startDate,endDate,money,description) values(?,?,?,?,?,?)";
        Object[] obj = {businesses.getbName(),
                businesses.getStatus(),
                businesses.getStartDate(),
                businesses.getEndDate(),
                businesses.getMoney(),
                businesses.getDescription()};
        return db.dtUpdate(sql, obj);
    }

    //业务信息查询(根据业务ID)
    @Override
    public ArrayList<Businesses> selectBusiness(Integer bId) {
        String sql = "select * from businesses where bId=?";
        return db.dtQuery(sql, Businesses.class, bId);
    }

    //业务信息修改(根据业务对象)
    @Override
    public int updateBusiness(Businesses businesses) {
        String sql = "update businesses set bName=?," +
                "description=?," +
                "startDate=?," +
                "endDate=?," +
                "status=?," +
                "money=?" +
                " where bId=?";
        Object[] obj = {businesses.getbName(),
                businesses.getDescription(),
                businesses.getStartDate(),
                businesses.getEndDate(),
                businesses.getStatus(),
                businesses.getMoney(),
                businesses.getbId()};
        return db.dtUpdate(sql, obj);
    }

    @Override
    public int deleteBusiness(Integer bId) {
        String sql="delete from businesses where bId=?";
        return db.dtUpdate(sql,bId);
    }
}
